package cn.edu.gdpt.mobile0123456789hwh0526;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

public class ProviderAdapter {
    private Context context;

    public ProviderAdapter(Context context) {
        this.context = context;
    }
    public void insert(String name,int age,float score){
        ContentValues values=new ContentValues();//定义映射对象，并赋值
        values.put("name",name);
        values.put("age",age);
        values.put("score",score);
//        通过Uri解析内容提供者地址
        Uri uri=Uri.parse("content://cn.edu.gdpt.mobile0123456789hwh0526.StudentProvider/insert");
        ContentResolver resolver=context.getContentResolver();//获得ContentResolver对象
        resolver.insert(uri,values);//调用ContentResolver的方法对数据进行操作
    }
    public Cursor QueryAll(){
        //        通过Uri解析内容提供者地址
        Uri uri=Uri.parse("content://cn.edu.gdpt.mobile0123456789hwh0526.StudentProvider/query");
        ContentResolver resolver=context.getContentResolver();//获得ContentResolver对象
        return resolver.query(uri,null,null,null,null);
    }
    public int update(String name,int age,float score){
        ContentValues values=new ContentValues();//定义映射对象，并赋值
        values.put("age",age);
        values.put("score",score);
        Uri uri=Uri.parse("content://cn.edu.gdpt.mobile0123456789hwh0526.StudentProvider/update");
        ContentResolver resolver=context.getContentResolver();//获得ContentResolver对象
        return resolver.update(uri,values,"name=?",new String[]{name});//根据姓名更新数据
    }
    public int delete(String name){
        Uri uri=Uri.parse("content://cn.edu.gdpt.mobile0123456789hwh0526.StudentProvider/delete");
        ContentResolver resolver=context.getContentResolver();//获得ContentResolver对象
        return resolver.delete(uri,"name=?",new String[]{name});//根据姓名删除数据
    }
}
